<template>
	<view class="user" :class="[isDark?'dark':'', $store.state.AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar" :class="isDark?'dark':'bg-white'" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px','background-color':isDark?'#1c1c1c':'#ffffff'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('user.qrcodelogin.kjscanlogin')}}
				</view>
				<view class="action">
				</view>
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="qrcodeLogin">
			<view class="qrcodeLogin-logo">
				<image :src="logo" mode="widthFix"></image>
			</view>
			<view class="qrcodeLogin-name">
				{{$t('user.qrcodelogin.use') + appname + $t('user.qrcodelogin.scanquicklogin')}}
			</view>
			<view class="qrcodeLogin-code">
				<image :src="scanLoginUrl"></image>
			</view>
		</view>
	</view>
</template>

<script>
	import waves from '@/components/xxley-waves/waves.vue';
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				
				
				logo:"",
				token:"",
				appname:"",
				text:"",
				
				scanText:"",
				scanLoginUrl:"",
				
				timer:null,
				
				styleIndex:"",
			}
		},
		onHide() {
			var that = this;
			clearInterval(that.timer);
			that.timer = null;
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			if(localStorage.getItem('userinfo')){
				that.userInfo = JSON.parse(localStorage.getItem('userinfo'));
				that.userInfo.style = "background-image:url("+that.userInfo.avatar+");"
				that.group = that.userInfo.group;
			}else{
				that.userInfo =null;
			}
			if(localStorage.getItem('token')){
				that.token = localStorage.getItem('token');
			}else{
				that.token = "";
			}
		},
		onLoad(res) {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
			if(res.text){
				that.text = res.text;
			}
			that.styleIndex = that.$API.GetStyleIndex();
			that.appname = that.$API.GetAppName();
			that.logo = that.$API.GetLogo();
			var text = that.randomString(7)+new Date().getTime();
			that.scanText = text;
			that.scanLoginUrl =that.$API.getScan()+"?codeContent="+text;
			that.timer = setInterval(function () {
				that.getScan();
			}, 3000);
		},
		methods:{
			back(){
				var that = this;
				clearInterval(that.timer);
				that.timer = null;
				uni.navigateBack({
					delta: 1
				});
			},
			getScan(){
			  const that = this;
			  var data = {
			    'codeContent':that.scanText
			  }
			  that.$Net.request({
			  	url: that.$API.getScanStatus(),
			  	data:data,
			  	header:{
			  		'Content-Type':'application/x-www-form-urlencoded'
			  	},
			  	method: "get",
			  	dataType: 'json',
			  	success: function(res) {
					if(res.data.code==1){
						uni.showToast({
							title: that.$t('user.qrcodelogin.scanloginsuccess'),
							icon: 'none'
						})
						//保存用户信息
						clearInterval(that.timer);
						that.timer = null;
						localStorage.setItem('userinfo',JSON.stringify(res.data.data));
						localStorage.setItem('token',res.data.data.token);
						var timer = setTimeout(function() {
							var styleIndex = that.styleIndex;
							uni.redirectTo({
								url: '/pages/home/'+styleIndex
							})
							clearTimeout('timer')
						}, 1000)
					}
			  	},
			  	fail: function(res) {
			  		uni.showToast({
			  			title: that.$t('common.networkfail'),
			  			icon: 'none'
			  		})
			  	}
			  });
			},
			randomString(e) {
			    e = e || 32;
			    var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
			    a = t.length,
			    n = "";
			    for (var i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
			    return n
			},
		},
	}
</script>
<style>
	page{
		background-color: #fff;
	}
</style>